package com.zl.hyzg.controller;

import com.aliyuncs.exceptions.ClientException;
import com.sun.net.httpserver.Authenticator;
import com.zl.hyzg.domain.User;
import com.zl.hyzg.service.RegistService;
import com.zl.hyzg.util.MD5Utils;
import com.zl.hyzg.util.SendMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class RegistController {
    @Autowired
    private RegistService registService;
    @PostMapping("/regist")
    public String regist(@RequestBody User user,HttpSession session){
        user.setPassword(MD5Utils.setPwd(user.getPassword()));
        System.out.println(user);
        Integer result = registService.regist(user);

        session.setAttribute("phone", user.getAdv_phone());
        session.setAttribute("password", user.getPassword());

        if(result == 1){
            return "success";
        }else{
            return "fail";
        }
    }

    @PostMapping("/sendPhoneSms")
    public boolean PhoneSms(String phoneNumber) {
        System.out.println(phoneNumber);
        //每次调用生成一次四位数的随机数
        int newcode = (int) (Math.random() * 9999) + 100;

        String code = Integer.toString(newcode);
        //填写你需要测试的手机号码
        try {
            return SendMessage.sendSms(phoneNumber, code);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;

    }



@GetMapping("/check")
    public String check(String phone){
        User user = registService.check(phone);
        if(user==null){
            return "success";
        }else{
            return "false";
        }
    }


}
